#!/bin/bash
set -e
DEV=""
DIR="/home/gicg/videos"
BOOT_ID=$(cat /proc/sys/kernel/random/boot_id | cut -d- -f1)
mkdir -p "$DIR"

# 等待摄像头出现
while [ -z "$DEV" ]; do
    DEV=$(ls /dev/v4l/by-id/* 2>/dev/null | head -n1)
    [ -z "$DEV" ] && sleep 1
done

# 主循环：每 3 分钟一个片段
while true; do
    TS=$(date +%Y%m%d-%H%M%S)
    FILE="$DIR/cam_${BOOT_ID}_${TS}.mkv"
    ffmpeg -f v4l2 -i "$DEV" \
           -c:v libx264 -preset ultrafast \
           -t 00:03:00 -reset_timestamps 1 -strftime 1 \
           -f matroska "$FILE"
done